From 010a120351e410ba5cd53eb439dc4a211630e965 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 19 Jun 2015 09:28:09 -0400 Subject: [PATCH] Add a font features demo This started life as a standalone tool, but it is nicer to have it integrated in gtk3-demo. --- demos/gtk-demo/Makefile.am | 1 + demos/gtk-demo/demo.gresource.xml | 3 + demos/gtk-demo/font-features.ui | 1346 +++++++++++++++++++++++++++++ demos/gtk-demo/font_features.c | 202 +++++ 4 files changed, 1552 insertions(+) create mode 100644 demos/gtk-demo/font-features.ui create mode 100644 demos/gtk-demo/font_features.c diff --git a/demos/gtk-demo/Makefile.am b/demos/gtk-demo/Makefile.am index 43cba929f2..6fcfa3100e 100644 --- a/demos/gtk-demo/Makefile.am +++ b/demos/gtk-demo/Makefile.am @@ -25,6 +25,7 @@ demos = \ entry_completion.c \ event_axes.c \ expander.c \ + font_features.c \ gestures.c \ glarea.c \ headerbar.c \ diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml index c364e42487..f05d86c94a 100644 --- a/demos/gtk-demo/demo.gresource.xml +++ b/demos/gtk-demo/demo.gresource.xml @@ -183,4 +183,7 @@ glarea-fragment.glsl glarea-vertex.glsl + + font-features.ui + diff --git a/demos/gtk-demo/font-features.ui b/demos/gtk-demo/font-features.ui new file mode 100644 index 0000000000..b378a10005 --- /dev/null +++ b/demos/gtk-demo/font-features.ui @@ -0,0 +1,1346 @@ + + + + + + False + 600 + 500 + + + True + True + Font Features + + + True + True + True + Reset + + + + True + False + view-refresh-symbolic + + + + + start + + + + + + + True + False + + + True + True + never + + + True + False + + + True + False + 10 + 10 + 10 + 10 + vertical + 6 + + + True + True + True + Sans 12 + + + + False + True + 0 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Enabled + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + + + True + False + Kerning + + + + + False + True + 1 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Common + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Discretionary + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 1 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Historical + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 2 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Contextual + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 3 + + + + + + + True + False + Ligatures + + + + + False + True + 2 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Small Caps + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Small Caps from Caps + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 1 + + + + + + + True + False + Letter Case + + + + + False + True + 3 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + Default + True + True + False + 0 + True + True + + + + False + True + 0 + + + + + Lining + True + True + False + 0 + True + True + numcasedefault + + + + False + True + 1 + + + + + Old-Style + True + True + False + 0 + True + True + numcasedefault + + + + False + True + 2 + + + + + + + True + False + Number Case + + + + + False + True + 4 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + Default + True + True + False + 0 + True + True + + + + False + True + 0 + + + + + Proportional + True + True + False + 0 + True + True + numspacedefault + + + + False + True + 1 + + + + + Tabular + True + True + False + 0 + True + True + numspacedefault + + + + False + True + 2 + + + + + + + True + False + Number Spacing + + + + + False + True + 5 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + Off + True + True + False + 0 + True + True + + + + False + True + 0 + + + + + Normal + True + True + False + 0 + True + True + fractiondefault + + + + False + True + 1 + + + + + Alternate + True + True + False + 0 + True + True + fractiondefault + + + + False + True + 2 + + + + + + + True + False + Fractions + + + + + False + True + 6 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Slashed Zero + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Alt. Annotation + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 1 + + + + + + + True + False + Numeric Extras + + + + + False + True + 7 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Swash + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Contextual + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 1 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Historical + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 2 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Stylistic + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 3 + + + + + + + True + False + Character Alternatives + + + + + False + True + 8 + + + + + True + True + + + True + False + 20 + 20 + 10 + vertical + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Set 1 + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 0 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Set 2 + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 1 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Set 3 + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 2 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Set 4 + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 3 + + + + + True + False + + + True + True + False + 0 + True + + + + False + True + 0 + + + + + Set 5 + True + True + False + 0 + True + + + + + False + True + 1 + + + + + False + True + 4 + + + + + + + True + False + Alternative Stylistic Sets + + + + + False + True + 9 + + + + + + + + + + False + True + 0 + + + + + True + False + vertical + + + True + 20 + 20 + 20 + 20 + + + True + + + + True + False + True + 0 + 0 + start + + + + + label + + + + + True + Lucky affluent actor asks to feast on giant 10.34" cheese-filled quiche in fjord. + + + start + + + entry + + + + + True + True + 0 + + + + + True + False + 20 + 20 + 20 + 0 + + + False + True + 1 + + + + + True + True + 1 + + + + + + diff --git a/demos/gtk-demo/font_features.c b/demos/gtk-demo/font_features.c new file mode 100644 index 0000000000..7e8cec8138 --- /dev/null +++ b/demos/gtk-demo/font_features.c @@ -0,0 +1,202 @@ +/* Font Features + * + * This demonstrates support for OpenType font features with + * Pango attributes. The attributes can be used manually or + * via Pango markup. + */ + +#include + +static GtkWidget *label; +static GtkWidget *settings; +static GtkWidget *font; +static GtkWidget *resetbutton; +static GtkWidget *numcasedefault; +static GtkWidget *numspacedefault; +static GtkWidget *fractiondefault; +static GtkWidget *stack; +static GtkWidget *entry; + +static GtkWidget *toggle[24]; + +static void +update (void) +{ + GString *s; + char *font_desc; + char *font_settings; + const char *text; + gboolean has_feature; + int i; + + text = gtk_entry_get_text (GTK_ENTRY (entry)); + + font_desc = gtk_font_chooser_get_font (GTK_FONT_CHOOSER (font)); + + s = g_string_new (""); + + has_feature = FALSE; + for (i = 0; i < 24; i++) + { + if (!gtk_widget_is_sensitive (toggle[i])) + continue; + + if (GTK_IS_RADIO_BUTTON (toggle[i])) + { + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle[i]))) + { + if (has_feature) + g_string_append (s, ", "); + g_string_append (s, gtk_buildable_get_name (GTK_BUILDABLE (toggle[i]))); + g_string_append (s, " 1"); + has_feature = TRUE; + } + } + else + { + if (has_feature) + g_string_append (s, ", "); + g_string_append (s, gtk_buildable_get_name (GTK_BUILDABLE (toggle[i]))); + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle[i]))) + g_string_append (s, " 1"); + else + g_string_append (s, " 0"); + has_feature = TRUE; + } + } + + font_settings = g_string_free (s, FALSE); + + gtk_label_set_text (GTK_LABEL (settings), font_settings); + + s = g_string_new (""); + g_string_append_printf (s, "%s", font_desc, font_settings, text); + + gtk_label_set_markup (GTK_LABEL (label), s->str); + + g_string_free (s, TRUE); + + g_free (font_desc); + g_free (font_settings); +} + +static void +reset (void) +{ + int i; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numcasedefault), TRUE); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numspacedefault), TRUE); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fractiondefault), TRUE); + for (i = 0; i < 24; i++) + { + if (!GTK_IS_RADIO_BUTTON (toggle[i])) + { + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle[i]), FALSE); + gtk_widget_set_sensitive (toggle[i], FALSE); + } + } +} + +static char *text; + +static void +switch_to_entry (void) +{ + text = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry))); + gtk_stack_set_visible_child_name (GTK_STACK (stack), "entry"); +} + +static void +switch_to_label (void) +{ + g_free (text); + text = NULL; + gtk_stack_set_visible_child_name (GTK_STACK (stack), "label"); + update (); +} + +static gboolean +entry_key_press (GtkEntry *entry, GdkEventKey *event) +{ + if (event->keyval == GDK_KEY_Escape) + { + gtk_entry_set_text (GTK_ENTRY (entry), text); + switch_to_label (); + return GDK_EVENT_STOP; + } + + return GDK_EVENT_PROPAGATE; +} + +GtkWidget * +do_font_features (GtkWidget *do_widget) +{ + static GtkWidget *window = NULL; + + if (!window) + { + GtkBuilder *builder; + int i; + + builder = gtk_builder_new_from_resource ("/font-features/font-features.ui"); + + gtk_builder_add_callback_symbol (builder, "update", update); + gtk_builder_add_callback_symbol (builder, "reset", reset); + gtk_builder_add_callback_symbol (builder, "switch_to_entry", switch_to_entry); + gtk_builder_add_callback_symbol (builder, "switch_to_label", switch_to_label); + gtk_builder_add_callback_symbol (builder, "entry_key_press", G_CALLBACK (entry_key_press)); + gtk_builder_connect_signals (builder, NULL); + + window = GTK_WIDGET (gtk_builder_get_object (builder, "window")); + label = GTK_WIDGET (gtk_builder_get_object (builder, "label")); + settings = GTK_WIDGET (gtk_builder_get_object (builder, "settings")); + resetbutton = GTK_WIDGET (gtk_builder_get_object (builder, "reset")); + font = GTK_WIDGET (gtk_builder_get_object (builder, "font")); + numcasedefault = GTK_WIDGET (gtk_builder_get_object (builder, "numcasedefault")); + numspacedefault = GTK_WIDGET (gtk_builder_get_object (builder, "numspacedefault")); + fractiondefault = GTK_WIDGET (gtk_builder_get_object (builder, "fractiondefault")); + stack = GTK_WIDGET (gtk_builder_get_object (builder, "stack")); + entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry")); + + i = 0; + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "kern")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "liga")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "dlig")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "hlig")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "clig")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "smcp")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "c2sc")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "lnum")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "onum")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "pnum")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "tnum")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "frac")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "afrc")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "zero")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "nalt")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "swsh")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "calt")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "hist")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "salt")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "ss01")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "ss02")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "ss03")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "ss04")); + toggle[i++] = GTK_WIDGET (gtk_builder_get_object (builder, "ss05")); + + update (); + } + + if (!gtk_widget_get_visible (window)) + { + gtk_window_present (GTK_WINDOW (window)); + } + else + { + gtk_widget_destroy (window); + window = NULL; + } + + return window; +} -- 2.30.2